const axios = require("axios");
const NProgress =require("nprogress");
let appglobal = require("../../appglobal.js");
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.interceptors.request.use(config => {
//     let append = document.getElementsByName('body')
//     append.innerHTML = '<img style="position:fixed;\n' +
//         ' left:47%;\n' +
//         ' top:40%;\n' +
//         ' transform: translateY(-50%),translateX(-50%);"' +
//         ' src="../server/1.jpg"/>'
//     return config
// }, err => {
//     return Promise.resolve(err);
// });
// axios请求拦截器
NProgress.configure({minimum:0.3});
NProgress.configure({ trickleRate: 0.02, trickleSpeed: 800 });
axios.interceptors.request.use(
    config => {
        NProgress.start() // 设置加载进度条(开始..)
        return config
    },
    error => {
        return Promise.reject(error)
    }
)
// axios响应拦截器
axios.interceptors.response.use(
    function (response) {
        NProgress.done() // 设置加载进度条(结束..)
        return response
    },
    function (error) {
        return Promise.reject(error)
    }
)

////请求服务端的域名配置
const base = {
    apibd: appglobal.serverApiUrl
}
//post的统一方法
function httpPost(url, params, config = {}) {
    return axios({
        url: url,
        method: 'post',
        data: {
            ...params
        },
        ...config
    });
}

module.exports = {
    initview: params => httpPost(`${base.apibd}/image/initScan`, params),
    createImageDocument: params => httpPost(`${base.apibd}/image/createImageDocument`, params),
    paralleliz: axiosarr => axios.all(axiosarr),
    httpGetTest: (params) => {
        return axios({
            url: "http://127.0.0.1:3000/test/downloadfile",
            method: 'get',
            data: {},
            onDownloadProgress(progress) {
                // console.log(Math.round(progress.loaded / progress.total * 100) + '%');
                console.log(progress);
            }
        });
    },
    fileupload: (formdata, config) => {
        return axios({
            url: `${base.apibd}/file/fileupload`,
            method: 'post',
            data: formdata,
            headers: {
                'Content-Type': 'mutlipart/form-data'
            },
            ...config
        });
    }
}